package com.adhere.system.service.impl;

import com.adhere.system.mapper.RbacPrivilegeMapper;
import com.adhere.system.service.IRbacPrivilegeService;
import com.adhere.system.pojo.RbacPrivilege;
import com.adhere.system.pojo.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * rbac_privilege表行数据执行“逻辑真删除” 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-04-02
 */
@Service
public class RbacPrivilegeServiceImpl extends ServiceImpl<RbacPrivilegeMapper, RbacPrivilege> implements IRbacPrivilegeService {

    @Autowired
    private RbacPrivilegeMapper privilegeMapper;


    @Override
    public PageVo<RbacPrivilege> getPrivilegeByPage(Integer currentPage, Integer pageSize, String name) {

        Page<RbacPrivilege> page = new Page<>(currentPage, pageSize);
        QueryWrapper<RbacPrivilege> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(name)) {
            queryWrapper.like("name", name);
        }
        queryWrapper.orderByDesc("create_time");
        Page<RbacPrivilege> resultPage = privilegeMapper.selectPage(page, queryWrapper);
        return new PageVo<RbacPrivilege>()
                .setCurrentPage(currentPage)
                .setPageSize(pageSize)
                .setTotal(resultPage.getTotal())
                .setObjectList(resultPage.getRecords());
    }

    /**
     * 根据角色code查询权限列表
     * @param roleCode
     * @return
     */
    @Override
    public List<RbacPrivilege> getPrivilegeListByRoleCode(String roleCode) {
        return privilegeMapper.getPrivilegeListByRoleCode(roleCode);
    }

    /**
     * 根据请求方式，协议，uri去查询权限的code
     * @param requestMethod
     * @param scheme
     * @param uri
     * @return
     */
    @Override
    public String getPrivilege(String requestMethod, String scheme, String uri) {
        QueryWrapper<RbacPrivilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("request_method", requestMethod);
        queryWrapper.eq("scheme", scheme);
        queryWrapper.eq("uri", uri);
        RbacPrivilege rbacPrivilege = privilegeMapper.selectOne(queryWrapper);
        return rbacPrivilege==null?null:rbacPrivilege.getCode();
    }

    /**
     * 根据角色code列表查询权限列表
     * @param roleCodeList
     * @return
     */
    @Override
    public List<RbacPrivilege> getPrivilegeByRoleList(List<String> roleCodeList) {
        return privilegeMapper.getPrivilegeByRoleList(roleCodeList);
    }

    /**
     * 根据用户编码查询权限列表
     * @param userCode
     * @return
     */
    @Override
    public List<RbacPrivilege> getPrivilegeListByUserCode(String userCode) {
        return privilegeMapper.getPrivilegeListByUserCode(userCode);
    }
}
